package com.ecnu.jiagou.damai.controller;

import java.util.List;

import com.ecnu.jiagou.damai.entity.Product;
import com.ecnu.jiagou.damai.entity.Ticket;
import com.ecnu.jiagou.damai.result.CodeMsg;
import com.ecnu.jiagou.damai.result.Result;
import com.ecnu.jiagou.damai.service.OrderService;
import com.ecnu.jiagou.damai.utils.RedisUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;


@RestController
@RequestMapping("/order")
public class OrderController {
    
    @Autowired
    private OrderService orderService;

    @Autowired
    private RedisUtil redisUtil;

    @GetMapping("/info")
    public Result<Ticket> getTicketInfo(Integer ticketid) {  //====Integer maybe cause error
        if(redisUtil.get(("ticket"+ ticketid)) != null){
            Ticket t = new Ticket();
            String[] res = redisUtil.get("ticket"+ticketid).toString().split(";");
            t.setTicketid(ticketid);            
            t.setSessionid(Long.parseLong(res[0]));
            t.setPrice(Float.parseFloat(res[1]));
            t.setSeat(res[2]);
            t.setCount(Integer.parseInt(res[3]));
            
            return Result.success(t);
        }
        Ticket ticket = orderService.getTicketById(ticketid);
        redisUtil.set("ticket"+ticketid, ticket.getSessionid()+";"+ticket.getPrice()+";"+ticket.getSeat()+";"+ticket.getCount());
        return Result.success(ticket);
    }
    /*
    @PostMapping("/add")
    @Transactional
    public Result<Boolean> addOrder(Long userid,String ticketid, String rows, String cols) {

        String[] row = rows.split(",");
        String[] col = cols.split(",");
        Ticket ticket = orderService.getTicketById(ticketid);
        String seats = ticket.getSeat();
        String[] seat = seats.split(",");
        int count = ticket.getCount();
        if(row.length > count){
            return Result.error(CodeMsg.ERROR);
        }
        //boolean result = orderService.updateTicket(ticket);
        for(int i = 0; i < row.length; ++i){
            int m = Integer.parseInt(row[i]), n = Integer.parseInt(col[i]);
            StringBuilder sb = new StringBuilder(seat[m-1]);
            if(sb.charAt(n) != '1'){
                return Result.error(CodeMsg.ERROR);
            }
            sb.replace(n, n+1, "2");
            seat[m-1] = sb.toString();
            count --;
        }
        //System.out.println(seat[0]);
        String newseat = StringUtils.join(seat, ",");
        ticket.setSeat(newseat);
        ticket.setCount(count);
        boolean result = orderService.updateTicket(ticket);

        //String orderid = UUIDUtil.uuid().toUpperCase();
        boolean result1 = orderService.updateTicket(ticketid, rows, cols);
        if(result1){
            boolean result2 = orderService.addOrder(userid, ticketid, rows, cols);
            if(result2){
                return Result.success(result1 && result2);
            }
        }
        return Result.error(CodeMsg.ERROR);
    }*/

    /**
     * 查询订单列表
     * @param userid
     * @return
     */
    @GetMapping("/get")
    public Result<List<Product>> query(Long userid){
        List<Product> list = orderService.getProduct(userid);
        return Result.success(list);
    }

    @PostMapping("/delete")
    public Result<Boolean> delete(String orderid){
        return orderService.deleteOrder(orderid);
    }

}
